package charlie.shop.admin;

import charlie.shop.misc.Common;
import com.charlie.beans.Groups;
import com.charlie.beans.Users;
import com.charlie.beans.UsersGroups;
import com.charlie.db.DAOLocal;
import com.charlie.sessions.GroupsFacadeLocal;
import com.charlie.sessions.UsersFacadeLocal;
import com.charlie.sessions.UsersGroupsFacadeLocal;
import java.io.IOException;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Add new user servlet
 * 
 * @author Оленка
 */
@WebServlet(name = "AddUsers", urlPatterns = {"/admin/addUsers"})
public class AddUsers extends HttpServlet {

    @EJB
    private GroupsFacadeLocal groupsFacade;
    @EJB
    private UsersFacadeLocal usersFacade;
    @EJB
    private UsersGroupsFacadeLocal usersGroupsFacade;
    @EJB
    private DAOLocal dao;
    
    Groups group;

    /**
     * Return groups and users information
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        int gId = Common.getParametrInt(request, "groupId");
        group = groupsFacade.findByGroupsId(Integer.valueOf(gId));
        request.setAttribute("group", group);
        request.setAttribute("allUsers", dao.getUsersRange(1, usersFacade.count()));
        //id of users who are in group
        request.setAttribute("groupUsersId", dao.getUserIdByGroupId(gId));
        response.setContentType("text/html;charset=UTF-8");
        getServletContext().getRequestDispatcher("/admin/addUsers.jsp").forward(request, response);
    }

     /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Add user to the group
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String select[] = request.getParameterValues("id");
        if (select != null) {
            if (select.length != 0) {
                for (String uId : select) {
                    UsersGroups ug = new UsersGroups();
                    Users u = usersFacade.findByUsersId(Integer.valueOf(uId));
                    ug.setUsersId(u);
                    ug.setGroupsId(group);
                    usersGroupsFacade.create(ug);
                }
            }
        }
        processRequest(request, response);
    }  
}
